package com.imooc.service.impl;

import com.imooc.config.WechatAccountConfig;
import com.imooc.dto.OrderDTO;
import com.imooc.service.PushMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class PushMessageServiceImpl implements PushMessageService {

    @Autowired
    private WxMpService wxMpService;

    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Override
    public void orderStatus(OrderDTO orderDTO) {
        WxMpTemplateMessage templateMessage = new WxMpTemplateMessage();
        templateMessage.setTemplateId(wechatAccountConfig.getTemplateId());
//        templateMessage.setToUser("wxb7d452a6ff0d3ab1");
        templateMessage.setToUser(orderDTO.getBuyerOpenid());

        List<WxMpTemplateData> data = Arrays.asList(
            new WxMpTemplateData("first","亲，记得收货哦"),
            new WxMpTemplateData("keyword1","微信点餐"),
            new WxMpTemplateData("keyword2","189988935"),
            new WxMpTemplateData("keyword3","wx5e00123f322f6202"),
            new WxMpTemplateData("keyword4", orderDTO.getOrderStatusEnum().getMsg()),
            new WxMpTemplateData("keyword5", "￥" + orderDTO.getOrderAmount()),
            new WxMpTemplateData("remark","欢迎再次光临")
        );
        templateMessage.setData(data);
        try{
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        }catch (WxErrorException e){
            log.error("[模板消息] 发送失败{}",e);
        }
    }
}
